package stud.mpgame.flash.game;

import flash.utils.TypedDictionary;

/**
 * ...
 * @author gdsg
 */

enum GameEvent {
     playerAdded;
     playerRemoved;
}
 
class GameEventDispatcher {
    
    private var listeners:TypedDictionary < GameEvent, Array<GameEvent->Dynamic->Void>>;
     
    public function new() { 
        
        listeners = new TypedDictionary< GameEvent, Array<GameEvent->Dynamic->Void>>();
    
    }
    
    public function addGameEventListener(event:GameEvent, func:GameEvent->Dynamic->Void):Void {
        var list = listeners.get(event);
        if (list == null){
            list = new Array < GameEvent->Dynamic->Void > ();
            listeners.set(event, list);
        }
        list.push(func);  
    }
    
    public function removeGameEventListener(event:GameEvent, func:GameEvent->Dynamic->Void):Void {
        if (listeners.event!=null) {
            listeners.get(event).remove(func);
        }
    }
    
    public function notifyListeners(event:GameEvent, data:Dynamic) {
        var list:Array < GameEvent->Dynamic->Void > = listeners.get(event);
        
        for (listener in list) {
            listener(event,data);
        }
    }
    
}